const Mock = require('mockjs')

const List = []
const count = 100

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    timestamp: +Mock.Random.date('T'),
    author: '@first',
    reviewer: '@first',
    title: '@title(5, 10)',
    content_short: 'mock data',
    content: '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>',
    forecast: '@float(0, 100, 2, 2)',
    importance: '@integer(1, 3)',
    'type|1': ['CN', 'US', 'JP', 'EU'],
    'status|1': ['published', 'draft'],
    display_time: '@datetime',
    comment_disabled: true,
    pageviews: '@integer(300, 5000)',
    image_uri: 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3',
    platforms: ['a-platform']
  }))
}

module.exports = [{
    url: '/admin/article/query',
    type: 'get',
    response: config => {
      // 请求参数
      const {
        type,
        title,
        current = 1,
        length = 10,
      } = config.query

      // 条件过滤
      let mockList = List.filter(item => {
        if (type && item.type !== type) return false
        if (title && item.title.indexOf(title) < 0) return false
        return true
      })

      // 分页
      const pageList = mockList.filter((item, index) => index < length * current && index >= length * (current - 1))

      return {
        data: pageList,
        draw: 1,
        startIndex: 1,
        pageSize: 10,
        pages: 10,
        recordsFiltered: 100,
        recordsTotal: 100,
      }
    }
  },

  {
    url: '/admin/article/getEntity',
    type: 'get',
    response: config => {
      const {
        id
      } = config.query
      for (const article of List) {
        if (article.id === +id) {
          return article
        }
      }
    }
  },

  {
    url: '/admin/article/save',
    type: 'post',
    response: _ => {
      return {
        code: null,
        success: true,
        message: null,
      }
    }
  },

  {
    url: '/admin/article/delete',
    type: 'post',
    response: _ => {
      return {
        code: null,
        success: true,
        message: null,
      }
    }
  }
]
